#include "../share/object.h"

#include "sceneobj_loop.h"
#include "sceneobj_manager.h"
#include "scene_logic.h"
#include "GameServer.h"
#include "../share/time_module.h"

using namespace common;

bool CSceneObjLoop::PlayerLoopTimeEvent( CObj& obj, void* param )
{
	Player& player = ( Player& )obj;
	int* count = ( int* )param;

// 	bool check_ping = player.CheckPingTime( );
// 	if( check_ping == false )
// 	{
// 		if( player.connected( ) == true )
// 		{
// 			//检测ping出现问题，那么关闭连接;
// 			CSceneLogic::GetInstance( ).CloseTcpConnect( player.GetSocketId() );
//             if( player.player_state( ) != CPlayer::STATE_ZOMBIE )
//             {
//                 player.OnDisconnect( );
//             }
//             else
//             {
//                 player.set_connected( false );
//             }
// 			LOG_ERROR("default","close connect of role %d because out of ping", player.GetPlayerId( ) );
// 		}	
// 	}

	if( player.PlayerInGame( ) == false )
	{
		return true;
	}

	int32_t player_last_save = player.GetLastSaveTime( );
	if ( player_last_save == 0 )
	{
		player.RefreshLastSaveTime();
		return false;
	}

	int32_t now = CUR_SEC;
	
	// 60秒存储一次玩家数据

	int32_t next_save = player_last_save + 300;
	if( next_save < now )
	{
 		player.PlayerSave( );
 		player.RefreshLastSaveTime( );
		LOG_DEBUG("default","save player[%d],next[%d],last[%d],now[%d]", player.GetPlayerId(),next_save, player_last_save, now);
		( *count )++;
	}

	// 一次存储60个
	if( ( *count ) >= 12 )
	{
		return false;
	}

	return true;
}

bool CSceneObjLoop::PlayerLoopLogout( CObj& obj, void* param )
{
	if( param != NULL )
	{
		int& count = *( ( int* )param );
		count++;
		if( count >= 50 )
		{
			return false;
		}
	}

	Player& player = ( Player& )obj;
	
	player.KickPlayerNotify(2);
	CSceneLogic::GetInstance( ).PlayerLeaveGame( player );

	return true;
}